package com.cskaoyan.salary.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {

    private static Properties properties;

    static {
        InputStream inputStream = null;
        try {
            //在ee项目里，这个路径获取到的是tomcat的bin目录
            //类外侧的package是一个整体   java com.cskaoyan.login.xxxxx
            inputStream = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
            //inputStream = new FileInputStream(new File("jdbc.properties"));
            properties = new Properties();
            properties.load(inputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driverClassName");
        String url = properties.getProperty("url");
        Class.forName(driver);
        return DriverManager.getConnection(url, user, password);
    }


    public static void releaseResources(Connection connection, ResultSet resultSet, Statement statement) throws SQLException {
            if(connection != null) {
                connection.close();
            }
            if(statement != null){
                statement.close();
            }
            if(resultSet != null){
                resultSet.close();
            }
    }
}
